package com.ocom.webapi.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.card.EntityXfChannelInfo;
import com.ocom.common.entity.card.EntityXfMerchPayInfo;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.enums.GzhEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.AddConfigureInfoRequest;
import com.ocom.common.request.payconfig.DeleteConfigureInfoRequest;
import com.ocom.common.request.payconfig.GetConfigureInfoRequest;
import com.ocom.common.request.payconfig.UpdateConfigureInfoRequest;
import com.ocom.common.request.web.OneConfigureInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.RandomUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.webapi.business.PayCenterService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.po.PayCenterVo;
import com.ocom.webapi.vo.PayCenterCompanyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

//支付通道配置

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class ConfigurePageController {

	@Autowired
	private PayCenterService payCenterService;

	@Autowired
	private PayConfigClientService payConfigClientService;

	@PostMapping(WebApiServiceUrl.URL_ConfigurePageOnShow)
	public Result onShow(@RequestBody GetConfigureInfoRequest request) {
		return payConfigClientService.getConfigureInfo(request);
	}

	@PostMapping(WebApiServiceUrl.URL_ConfigurePageInfo)
	public Result info(@RequestBody OneConfigureInfoRequest request) {
		Result result = payConfigClientService.configureInfo(request);
		EntityXfChannelInfo entityXfChannelInfo = JSON.parseObject(JSON.toJSONString(result.getData()), EntityXfChannelInfo.class);
		if (result.getCode() != 200) {
			return result;
		}

		Map<String, Object> map = new LinkedHashMap<>();
		map.put("info", entityXfChannelInfo);

		try {
			Result result1 = payConfigClientService.MerchPageInfo(request.getComId(), entityXfChannelInfo.getMerchId());
			if (result1.getCode() != 200) {
				log.info("获取一条信息异常 【{}】", result1);
				return ResultUtil.success(map);
			}
			JSONObject jsonGetDeviceFlag = JSON.parseObject(JSON.toJSONString(result1.getData()));
			Long companyId = jsonGetDeviceFlag.getLong("companyId");
			if (companyId == null) {
				map.put("payCenterVo", null);
			} else {
				PayCenterVo payCenterVo = payCenterService.payChannelList(request.getComId(),companyId.toString());
				map.put("payCenterVo", payCenterVo.getData());
			}
		} catch (Exception e) {
			log.info("获取详情通道异常 【{}】", e);
		}
		return ResultUtil.success(map);
	}


	@PostMapping(WebApiServiceUrl.URL_ConfigurePageAdd)
	public Result add(@RequestBody AddConfigureInfoRequest request) {
		//添加商户
		Result result = payConfigClientService.MerchPageInfo(request.getComId(), request.getMerchId());
		if (result.getCode() != 200) {
			return result;
		}
		EntityXfMerchantInfo entityXfMerchantInfo = JSON.parseObject(JSON.toJSONString(result.getData()), EntityXfMerchantInfo.class);
		if (CommonUtil.isNull(entityXfMerchantInfo.getCompanyId())) {
			entityXfMerchantInfo.setUserName(RandomUtil.getRandomString(8));
			entityXfMerchantInfo.setPassword(RandomUtil.getRandomString(8));

			//添加商户
			PayCenterCompanyVo payCenterCompanyVo = new PayCenterCompanyVo();
			payCenterCompanyVo.setCompanyName(entityXfMerchantInfo.getComId()+"#"+entityXfMerchantInfo.getMerchName());
			payCenterCompanyVo.setUserName(entityXfMerchantInfo.getUserName());
			payCenterCompanyVo.setPayPassword(entityXfMerchantInfo.getPassword());
			payCenterCompanyVo.setPayChannel("00000000000000000000");
			payCenterCompanyVo.setIsEverySet(false);
			payCenterCompanyVo.setLocked(false);
			payCenterCompanyVo.setContactName("3.0");
			payCenterCompanyVo.setContactNumber("15812902593");
			payCenterCompanyVo.setContactAddress("广东-广州-黄埔");
			PayCenterVo payCenterVo = payCenterService.addMerch(request.getComId(),payCenterCompanyVo);
			if (payCenterVo == null || !payCenterVo.getCode().equals("0000")) {
				return ResultUtil.error(ResultEnum.Err_2018);
			}
			Map<String, String> map = JSON.parseObject(JSON.toJSONString(payCenterVo.getData()), Map.class);
			entityXfMerchantInfo.setCompanyId(map.get("companyId"));

			EntityXfMerchPayInfo entityXfMerchPayInfo =new EntityXfMerchPayInfo();
			entityXfMerchPayInfo.setComId(entityXfMerchantInfo.getComId());
			entityXfMerchPayInfo.setMerchId(entityXfMerchantInfo.getMerchId());
			entityXfMerchPayInfo.setCompanyId(entityXfMerchantInfo.getCompanyId());
			entityXfMerchPayInfo.setUserName(entityXfMerchantInfo.getUserName());
			entityXfMerchPayInfo.setPassword(entityXfMerchantInfo.getPassword());
			Result result1 = payConfigClientService.addPayInfo(entityXfMerchPayInfo);
			if(result1.getCode()!=200){
				return result1;
			}
		}
		if (request.getLink() == 1) {
			// 调统一
			PayCenterVo payCenterVo = payCenterService.addPayChannel(entityXfMerchantInfo.getComId(),request,entityXfMerchantInfo.getCompanyId());

			log.info("添加通道-统一支付返回 请求参数【{}】  【{}】", request, payCenterVo);
			if (payCenterVo == null || !payCenterVo.getCode().equals("0000")) {
				return ResultUtil.error(ResultEnum.Err_2018);
			}
		}
		return payConfigClientService.addConfigureInfo(request);
	}


	@PostMapping(WebApiServiceUrl.URL_ConfigurePageModify)
	public Result modify(@RequestBody UpdateConfigureInfoRequest request) {

		OneConfigureInfoRequest oneConfigureInfoRequest = new OneConfigureInfoRequest();
		oneConfigureInfoRequest.setComId(request.getComId());
		oneConfigureInfoRequest.setId(request.getId());
		Result result2 = payConfigClientService.configureInfo(oneConfigureInfoRequest);
		if (result2.getCode() != 200) {
			return result2;
		}
		EntityXfChannelInfo entityXfChannelInfo = JSON.parseObject(JSON.toJSONString(result2.getData()), EntityXfChannelInfo.class);

		//对接3.0
		if (entityXfChannelInfo.getLink() == 1) {
			Result result = payConfigClientService.MerchPageInfo(request.getComId(), request.getMerchId());
			if (result.getCode() != 200) {
				return ResultUtil.error(ResultEnum.Err_2031);
			}
			JSONObject jsonGetDeviceFlag = JSON.parseObject(JSON.toJSONString(result.getData()));
			Long companyId = jsonGetDeviceFlag.getLong("companyId");
			AddConfigureInfoRequest addConfigureInfoRequest = new AddConfigureInfoRequest();
			addConfigureInfoRequest.setComId(companyId);
			addConfigureInfoRequest.setCompanyId(companyId.toString());
			addConfigureInfoRequest.setPayWay(request.getPayWay());
			addConfigureInfoRequest.setChannelParam(request.getChannelParam());
			addConfigureInfoRequest.setMerchId(request.getMerchId());
			addConfigureInfoRequest.setDeviceNo(request.getDeviceNo());
			addConfigureInfoRequest.setCreateUser(request.getUpdateUser());
			addConfigureInfoRequest.setPayType(request.getPayType());

			PayCenterVo payCenterVo = payCenterService.editPayChannel(request.getComId(),addConfigureInfoRequest);
			if (payCenterVo == null || !payCenterVo.getCode().equals("0000")) {
				log.info("编辑通道-统一支付返回 请求参数【{}】  【{}】", addConfigureInfoRequest, payCenterVo);
				return ResultUtil.error(ResultEnum.Err_2018);
			}
			System.out.println(payCenterVo);
		}

		return payConfigClientService.updateConfigureInfo(request);
	}

	@PostMapping(WebApiServiceUrl.URL_ConfigurePageDelete)
	public Result delete(@RequestBody DeleteConfigureInfoRequest request) {
		OneConfigureInfoRequest oneConfigureInfoRequest = new OneConfigureInfoRequest();
		oneConfigureInfoRequest.setComId(request.getComId());
		oneConfigureInfoRequest.setId(request.getId());
		Result result2 = payConfigClientService.configureInfo(oneConfigureInfoRequest);
		if (result2.getCode() != 200) {
			return result2;
		}
		EntityXfChannelInfo entityXfChannelInfo = JSON.parseObject(JSON.toJSONString(result2.getData()), EntityXfChannelInfo.class);

		if (entityXfChannelInfo.getLink() == 1) {
			// 调统一
			PayCenterVo payCenterVo = payCenterService.delPayChannel(request.getComId(),request);
			if (payCenterVo == null || !payCenterVo.getCode().equals("0000")) {
				log.info("删除-统一支付返回 请求参数【{}】 【{}】", request, payCenterVo);
				return ResultUtil.error(ResultEnum.Err_2018);
			}
		}
		return payConfigClientService.deleteConfigureInfo(request);
	}


	@PostMapping(WebApiServiceUrl.Url_GzhChannelListOnShow)
	public Result gzhChannelList() {
		return ResultUtil.success(GzhEnum.getGzhEnumMap());
	}

}
